package com.yc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 日期工具
 * <p>
 *
 * @author BAO
 * @since 2021/1/19 18:31
 */
public class DateUtil extends cn.hutool.core.date.DateUtil{

    /**
     * 转换时间
     */
    public static Integer convertDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            return null;
        }
        return (Integer) (int) (date.getTime() / 1000);
    }

    /**
     * 时间戳转字符串
     */
    public static String timestampToDateStr(Long timestamp) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return format.format(timestamp);
    }

    /**
     * 时间戳转日期
     */
    public static Date timestampToDate(Long timestamp) {
        return new Date(timestamp);
    }

    /**
     * 获取当月开始时间
     */
    public static Date getCurrentMouthStartDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取日期是哪周
     */
    public static int getWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int weekIndex = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekIndex == 0) {
            weekIndex = 7;
        }
        return weekIndex;
    }


    /**
     * 获取当前日期是哪周
     */
    public static int getCurrentWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int weekIndex = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekIndex == 0) {
            weekIndex = 7;
        }
        return weekIndex;
    }
}
